package api;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/user")
public class UserServlet extends HttpServlet {
    //统计用户有多少博文
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        //数据库中已经存在count了，要返回响应的话，是返回count的值
        //得到count的值
        UserDao userDao=new UserDao();
        String username=req.getParameter("username");
        //把count转成要求的json格式
        int count=userDao.Count(username);
        String jsonString=objectMapper.writeValueAsString(count);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
}
